package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableCourier;
import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.dao.impl.CourierDaoMysql;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JsonUtil;
import jdk.security.jarsigner.JarSigner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-08-02 14:41
 * @Modified By:
 */

public class CourierController {
    @ResponseBody("/courier/console2.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = CourierService.console();
        Message message = new Message();
        if (data.size()==0){
            message.setStatus(-1);
        }else{
            message.setStatus(0);
        }
        message.setData(data);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        //时间格式的转换
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier c: list){
            String intime = DateFormatUtil.format(c.getIntime());
            String outtime = DateFormatUtil.format(c.getOuttime());
            BootStrapTableCourier boot = new BootStrapTableCourier(c.getId(),c.getUsername(),c.getPhonenumber(),c.getCardid(),c.getPassword(),c.getSendnumber(),intime,outtime);
            list2.add(boot);
        }
        //查询总共的条数
        List<Map<String, Integer>> console = CourierService.console();
        int total = console.get(0).get("data_size");
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setTotal(total);
        data.setRows(list2);
        String json = JsonUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/find2.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Courier courier = CourierService.findByphonenumber(number);
        Message message = new Message();
        if (courier==null){
            message.setStatus(-1);
            message.setResult("手机号码不存在");
        }else{
            message.setStatus(0);
            message.setResult("查找成功");
        }
        message.setData(courier);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/update2.do")
    public String update1(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String phonenumber = req.getParameter("phonenumber");
        String cardid = req.getParameter("cardid");
        String password = req.getParameter("password");
        Courier courier = new Courier();
        courier.setUsername(username);
        courier.setPassword(password);
        courier.setPhonenumber(phonenumber);
        courier.setCardid(cardid);
        boolean flag = CourierService.update(id, courier);

        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/insert2.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        String phonenumber = req.getParameter("phonenumber");
        String cardid = req.getParameter("cardid");
        String password = req.getParameter("password");
        Courier courier = new Courier();
        courier.setUsername(username);
        courier.setPassword(password);
        courier.setPhonenumber(phonenumber);
        courier.setCardid(cardid);
        boolean flag = CourierService.insert(courier);
        Message message = new Message();
        if (flag){
                message.setStatus(0);
                message.setResult("新增成功！");
        }else{
            message.setStatus(-1);
            message.setResult("新增失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/courier/delete2.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flag = CourierService.delete(id);
        Message message = new Message();
        if (flag){
           message.setStatus(0);
           message.setResult("删除成功!");
        }else{
            message.setStatus(-1);
            message.setResult("删除失败!");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }

}
